<template>
  <el-form :model="loginForm" :rules="rules" ref="loginForm" label-position="left" label-width="0px" class="demo-ruleForm login-container">
    <h3 class="title">系统登录</h3>
    <el-form-item prop="account">
      账号：<el-input type="text" v-model="loginForm.account" auto-complete="off" placeholder="账号" ></el-input>
    </el-form-item>
    <el-form-item prop="checkPass">
      密码：<el-input type="password" v-model="loginForm.checkPass" auto-complete="off" placeholder="密码"></el-input>
    </el-form-item>
    <el-checkbox v-model="checked" checked class="remember">记住密码</el-checkbox>
    <el-form-item style="width:100%;">
      <el-button type="primary" style="width:100%;" @click.native.prevent="handleSubmit" :loading="logining">登录</el-button>
      <!--<el-button type="primary" style="width:45%;" @click.native.prevent="register">公司入驻</el-button>-->
    </el-form-item>
  </el-form>
</template>

<script>
/* 引用jwt*/
  import jwt from 'jsonwebtoken';
  export default {
    data() {
      return {
        logining: false,
        loginForm: {
          account: '东邪',
          checkPass: '123456'
        },
        rules: { // 表单项校验
          account: [
            { required: true, message: '请输入账号', trigger: 'blur' },
            //{ validator: validaePass }
          ],
          checkPass: [
            { required: true, message: '请输入密码', trigger: 'blur' },
            //{ validator: validaePass2 }
          ]
        },
        checked: true
      };
    },
    methods: {
      //处理登录请求
      handleSubmit() {
        this.$refs.loginForm.validate((valid) => { // 表单校验
          if (valid) {
            this.logining = true; // 开启忙等框
            var loginParams = {
              account: this.loginForm.account,
              password: this.loginForm.checkPass,
              type:0 //登录的员工界面，所以默认type为0
            };
            this.$http.post("/login",loginParams).then(res =>{
              if (res.data.success){
                // console.log(res)
                var token = res.data.data
                let jwt = require('jsonwebtoken'); //加载jsonwebtoken工具包
                let resultJwtJson = jwt.decode(token); //解析token

                // 将token值和username存放到游览器本地仓库中
                localStorage.setItem("token",token)
                // 解析后的原本的username，permissions，menus   将permissions，menus转为json格式，方便后面登录验证解析
                localStorage.setItem("username",resultJwtJson.payload.logininfo.username)
                localStorage.setItem("permissions",JSON.stringify(resultJwtJson.payload.permissions))
                localStorage.setItem("menus",JSON.stringify(resultJwtJson.payload.menus))

                //修改登录成功后跳转到首页
                this.$router.push({ path: '/echarts' });
                // 重新刷新页面
                location.reload();
              }else {
                this.$message.error(res.data.message);
              }
              this.logining = false;
            })
            /*
            //发请求获取token
            this.$http.post("/login/login", loginParams).then(res=>{
              if(res.data.success){
                var userData = res.data.data;
                //把token存储起来
                // localStorage.setItem("U-TOKEN",userData.token);
                localStorage.setItem("realName",userData.realName);
                localStorage.setItem("userPic",userData.avatar);
                //修改登录成功后跳转到首页
                this.$router.push({ path: '/echarts' });
                this.logining = false;
                return;
              }else{
                this.logining = false;
                this.$message.error(res.data.message);
              }
            }).catch(error => {
              this.$message.error(error.message);
              this.logining = false;
              return;
            });
            */

          } else {
            return this.$message.error("校验失败");
          }
        });
      },
      register(){
          //跳转到公司入驻界面
          this.$router.push({ path: '/register' });
      },

    }
  }

</script>

<style lang="scss" scoped>
  .login-container {
    /*box-shadow: 0 0px 8px 0 rgba(0, 0, 0, 0.06), 0 1px 0px 0 rgba(0, 0, 0, 0.02);*/
    -webkit-border-radius: 5px;
    border-radius: 5px;
    -moz-border-radius: 5px;
    background-clip: padding-box;
    margin: 180px auto;
    width: 350px;
    padding: 35px 35px 15px 35px;
    background: #fff;
    border: 1px solid #eaeaea;
    box-shadow: 0 0 25px #cac6c6;
    .title {
      margin: 0px auto 40px auto;
      text-align: center;
      color: #505458;
    }
    .remember {
      margin: 0px 0px 35px 0px;
    }
  }
</style>
